﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using Kiva7.DataRequests;
using Kiva7.DataClasses;
using System.Linq;

namespace Kiva7.ViewModels
{
    public class JournalEntryViewModel : BaseViewModel
    {
        public JournalEntryViewModel()
        {
            _request = new LenderJournalEntriesRequest();
            _request.OnResponse += new DataRequest<DataResponse.JournalEntriesResponse, DataClasses.JournalEntry>.DataRequestDelegate(request_OnResponse);

            _journalEntriesList = new JournalEntryList();
        }

        private readonly LenderJournalEntriesRequest _request;
        private readonly LoanQueue _loans = new LoanQueue();

        private readonly JournalEntryList _journalEntriesList;

        private List<JournalEntry> _JournalEntries = new List<JournalEntry>();

        private int _PagesCount;
        public int PagesCount
        {
            get { return _PagesCount; }
            set
            {
                if (_PagesCount != value)
                {
                    _PagesCount = value;
                    NotifyPropertyChanged("PagesCount");
                }
            }
        }


        private bool _isLoaded;
        public bool IsLoaded
        {
            get { return _isLoaded; }
            set
            {
                if (_isLoaded != value)
                {
                    _isLoaded = value;
                    NotifyPropertyChanged("IsLoaded");
                }
            }
        }

        public List<JournalEntry> Data
        {
            get
            {
                return _JournalEntries;

                //return _journalEntriesList.OrderByDescending(i => i.Date);
            }
        }

        public void Clear()
        {
            _request.Cancel();
            _journalEntriesList.Clear();
            _loans.Clear();
        }

        public void LoadJournalEntries(LoanList loanItems)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback((stateInfo)
                =>
                {

                    bool startQueue = _loans.Count == 0;

                    foreach (Loan loan in loanItems)
                    {
                        _loans.Enqueue(loan);
                    }

                    if (startQueue)
                    {
                        LoadNextJournalEntry();
                    }
                }));
        }

        private void LoadNextJournalEntry()
        {
            if (_loans.Count > 0)
            {
                Loan loan = _loans.Dequeue();
                _request.GetJournalEntries(loan.Id, false);
            }
            else
            {

                if (Data.Count == 0)
                {
                    //Deployment.Current.Dispatcher.BeginInvoke(()
                    //                                          =>
                    //                                              {

                    foreach (
                        JournalEntry journalEntry in
                            _journalEntriesList.OrderByDescending(i => i.Date))
                    {
                        Data.Add(journalEntry);
                    }
                    // });
                    IsLoaded = true;
                    PagesCount = 1;
                    NotifyPropertyChanged("Data");
                }
                //NotifyPropertyChanged("JournalEntries");
            }
        }

        private void request_OnResponse(object sender, DataRequest<DataResponse.JournalEntriesResponse, DataClasses.JournalEntry>.DataRequestEventArgs e)
        {
            if (!e.Cancelled
                && e.Exception == null)
            {
                foreach (JournalEntry entry in e.Response.JournalEntries.JournalEntryItems)
                {
                    _journalEntriesList.Add(entry);
                }
                NotifyPropertyChanged("JournalEntriesSorted");
                LoadNextJournalEntry();
            }
        }
    }
}
